﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace SystemWnioskujacy
{
    public partial class FileForm : Form
    {
        public string path = String.Empty;
        
        public FileForm()
        {
            InitializeComponent();
            SizeRestrictions();
        }

        public FileForm(string path)
        {
            InitializeComponent();
            SizeRestrictions();
            this.path = path;

            try
            {
                FileBox.Text = System.IO.File.ReadAllText(path);
            }
            catch (Exception ex)
            {

            }
        }

        private void SizeRestrictions()
        {
            // RichTextBox size and position
            FileBox.Size = new Size(this.Size.Width, this.Size.Height - 40 - 13);
            FileBox.Top = 0;

            // Labels size and position
            LineLabel.Top = FileBox.Size.Height;
            LineNumLabel.Top = FileBox.Size.Height;
            ColumnLabel.Top = FileBox.Size.Height;
            ColumnNumLabel.Top = FileBox.Size.Height;

            LineLabel.Left = 0;
            LineNumLabel.Left = LineLabel.Right;
            ColumnLabel.Left = LineNumLabel.Right;
            ColumnNumLabel.Left = ColumnLabel.Right;
        }

        private void FileForm_Resize(object sender, EventArgs e)
        {
            SizeRestrictions();
        }

        private void FileForm_Enter(object sender, EventArgs e)
        {
            MainForm MyParent = (MainForm)this.MdiParent;
            MenuStrip ms = (MenuStrip)MyParent.Controls["MainMenu"];
            ToolStripMenuItem ti1 = (ToolStripMenuItem)ms.Items["plikToolStripMenuItem"];
            ToolStripMenuItem ti2 = (ToolStripMenuItem)ms.Items["buforToolStripMenuItem"];

            if (path == String.Empty)
            {
                ti1.DropDownItems["zapiszToolStripMenuItem"].Enabled = false;
                ti1.DropDownItems["zapiszjakoToolStripMenuItem"].Enabled = true;
            }
            else
            {
                ti1.DropDownItems["zapiszToolStripMenuItem"].Enabled = true;
                ti1.DropDownItems["zapiszjakoToolStripMenuItem"].Enabled = true;
            }

            ti2.DropDownItems["wczytajToolStripMenuItem1"].Enabled = true;
        }

        private void FileForm_Leave(object sender, EventArgs e)
        {
            MainForm MyParent = (MainForm)this.MdiParent;
            MenuStrip ms = (MenuStrip)MyParent.Controls["MainMenu"];
            ToolStripMenuItem ti1 = (ToolStripMenuItem)ms.Items["plikToolStripMenuItem"];
            ToolStripMenuItem ti2 = (ToolStripMenuItem)ms.Items["buforToolStripMenuItem"];

            ti1.DropDownItems["zapiszToolStripMenuItem"].Enabled = false;
            ti1.DropDownItems["zapiszJakoToolStripMenuItem"].Enabled = false;
            ti2.DropDownItems["wczytajToolStripMenuItem1"].Enabled = false;
        }

        private void LineColumnNum(string text, int selection, Label Line, Label Column)
        {
            int fLine = 1, fColumn = 1;
            int i = 0;
            string[] nLine =
            {
                "\r\n",
                "\n"
            };

            for (i = 0; i <= selection; )
            {
                int j = -1;
                int k = 0;

                for (k = 0; j < i && k < nLine.Length; k++) j = FileBox.Text.IndexOf(nLine[k], i);
                if (k == nLine.Length) k--;

                if (j >= i && j < selection)
                {
                    fLine++;
                    i = j + nLine[k].Length;
                }
                else break;
            }

            fColumn = selection - i + 1;
            Line.Text = fLine.ToString();
            Column.Text = fColumn.ToString();
        }

        private void FileBox_SelectionChanged(object sender, EventArgs e)
        {
            LineColumnNum(FileBox.Text, FileBox.SelectionStart, LineNumLabel, ColumnNumLabel);
        }
    }
}
